-- Important before executing:
-- Replace %table% with the tablename you specified in config.yml.
-- Example: If your table is called lb-main, then:
-- RENAME TABLE `lb-main-extra` TO `lb-main-sign`;

-- Execute this once:
CREATE TABLE `players` (
  `playerid` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `playername` varchar(32) NOT NULL DEFAULT '-',
  PRIMARY KEY (`playerid`),
  UNIQUE (`playername`)
);
INSERT INTO `players` (`playername`) VALUES ('environment');

-- Excecute this for each table:

RENAME TABLE `%table%-extra` TO `%table%-sign`;
ALTER TABLE `%table%-sign` CHANGE COLUMN `extra` `signtext` TEXT;

ALTER TABLE `%table%` MODIFY `type` TINYINT UNSIGNED NOT NULL DEFAULT '0';
ALTER TABLE `%table%` MODIFY `replaced` TINYINT UNSIGNED NOT NULL DEFAULT '0';
ALTER TABLE `%table%` MODIFY `x` SMALLINT NOT NULL DEFAULT '0';
ALTER TABLE `%table%` MODIFY `y` TINYINT UNSIGNED NOT NULL DEFAULT '0';
ALTER TABLE `%table%` MODIFY `z` SMALLINT NOT NULL DEFAULT '0';

CREATE TABLE `%table%-chest` (
  `id` INT NOT NULL,
  `intype` SMALLINT UNSIGNED NOT NULL DEFAULT '0',
  `inamount` TINYINT UNSIGNED NOT NULL DEFAULT '0',
  `outtype` SMALLINT UNSIGNED NOT NULL DEFAULT '0',
  `outamount` TINYINT UNSIGNED NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
);

INSERT IGNORE INTO `players` (`playername`)
SELECT DISTINCT `player` 
FROM `%table%`;

ALTER TABLE `%table%` ADD `playerid` SMALLINT UNSIGNED NOT NULL DEFAULT '0' AFTER `player`;

UPDATE `%table%` INNER JOIN `players` ON `%table%`.`player` = `players`.`playername`
SET `%table%`.`playerid` = `players`.`playerid`;
ALTER TABLE `%table%` DROP `player`;